package zaphod.project1.model.internal;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.ui.PartInitException;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import zaphod.project1.model.IContainer;
import zaphod.project1.model.IItem;

public class Document extends ListContainer
{

    private Document()
    {

    }

    public static Document createDocumentFromFile( IFile file )
        throws CoreException
    {
        try
        {
            final Document document = new Document();
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser;
            parser = factory.newSAXParser();
            XMLReader producer = parser.getXMLReader();
            ContentHandler handler = new DocumentContentHandler( document );
            producer.setContentHandler( handler );
            InputSource input;
            input = new InputSource( file.getContents() );
            producer.parse( input );

            return document;
        }
        catch ( ParserConfigurationException e )
        {
            e.printStackTrace();
            throw new PartInitException( "ParserConfigurationException", e );
        }
        catch ( SAXException e )
        {
            e.printStackTrace();
            throw new PartInitException( "SAXException", e );
        }
        catch ( IOException e )
        {
            e.printStackTrace();
            throw new PartInitException( "IOException", e );
        }

    }

    private static final class DocumentContentHandler extends DefaultHandler
    {
        private IItem currentItem;

        private final String DOCUMENT = "document";
        private final String LIST = "list";
        private final String ITEM = "item";

        private final String P_NAME = "name";

        private DocumentContentHandler( Document document )
        {
            currentItem = document;
        }

        @Override
        public void startElement( String uri, String localName, String name,
            Attributes attributes ) throws SAXException
        {
            IItem newItem;
            if ( DOCUMENT.equals( name ) ) return;

            if ( ! ( currentItem instanceof IContainer ) )
                throw new SAXException( "unexpected non-container" );

            IContainer newCurrentItem = (IContainer) currentItem;

            if ( LIST.equals( name ) )
            {
                newItem = new ListContainer();
                newItem.setName( attributes.getValue( P_NAME ) );

                newCurrentItem.add( newItem );
                currentItem = newItem;
                return;
            }
            if ( ITEM.equals( name ) )
            {
                newItem = new Item();
                newItem.setName( attributes.getValue( P_NAME ) );

                newCurrentItem.add( newItem );
                currentItem = newItem;
                return;
            }

            throw new SAXException( "unexpected " + name );

        }

        @Override
        public void endElement( String uri, String localName, String name )
            throws SAXException
        {
            currentItem = currentItem.getParent();
        }
    }

}
